package com.feishi.core.util.common;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * Created by caixq on 4/26 026.
 */
public class ThreadUtil {
    /**
     * 线程数量不限，动态扩展至资源耗尽
     * */
    public static ExecutorService newCachedThreadPool(){
        return Executors.newCachedThreadPool();
    }
    /**
     * 固定线程数量
     * */
    public static ExecutorService newFixedThreadPool(int threadNum){
        return Executors.newFixedThreadPool(threadNum);
    }
    /**
     *单线程执行
     * */
    public static ExecutorService newSingleThreadExecutor(){
        return Executors.newSingleThreadExecutor();
    }
    /**
     *定时任务执行类
     * 启动固定线程数
     * */
    public static ScheduledExecutorService scheduledThreadPoolExecutor(int threadNum){
        return new ScheduledThreadPoolExecutor(threadNum);
    }
    public static void main(String[] args) {
        ThreadUtil util=new ThreadUtil();
        Random random=new Random();
        new Thread(()->{
            while (true){
                util.i=Math.abs(random.nextInt())+1;
            }
        }).start();
        final int[] i = {0};
        new Thread(()->{
            while (true){
              i[0] =  2/util.i;
            }
        }).start();
    }
     private int i=1;
}
